<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Stores_Entity extends CI_Model
{
    private $tablename = 'stores';
    private $id = null;
    private $title = null;
    private $name = null;
    private $note = null;
    private $created = null;
    private $order = null;
    private $acti = null;
    private $vip = null;
    private $status = null;
    private $status_info = null;
    private $date_exp = null;
    private $date_bg = null;
    
    public function setId($Id)
    {
        $this->id = $Id;
    }

    public function setTitle($Title)
    {
        $this->title = $Title;
    }

    public function setName($Name)
    {
        $this->name = $Name;
    }

    public function setNote($Note)
    {
        $this->note = $Note;
    }

    public function setCreated($Created)
    {
        $this->created = $Created;
    }

    public function setOrder($Order)
    {
        $this->order = $Order;
    }

    public function setActi($Acti)
    {
        $this->acti = $Acti;
    }

    public function setVip($Vip)
    {
        $this->vip = $Vip;
    }

    public function setStatus($Status)
    {
        $this->status = $Status;
    }

    public function setStatus_info($Status_info)
    {
        $this->status_info = $Status_info;
    }

    public function setDate_exp($Date_exp)
    {
        $this->date_exp = $Date_exp;
    }

    public function setDate_bg($Date_bg)
    {
        $this->date_bg = $Date_bg;
    }

    public function checkExistFK_orders($id)
    {
        $query = $this->db->get_where('orders', array('sid' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function checkExistFK_product_types($id)
    {
        $query = $this->db->get_where('product_types', array('sid' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function checkExistFK_advertises($id)
    {
        $query = $this->db->get_where('advertises', array('sid' => $id));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val == null || $val == '') continue;
            $params[$key] = $val;
        }
                
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        // check exist foreign key
        if ($this->checkExistFK_orders($id)) {
            return array('-1', 'stores'.' '.t('is exist in').' '.'orders');
        }
        // check exist foreign key
        if ($this->checkExistFK_product_types($id)) {
            return array('-1', 'stores'.' '.t('is exist in').' '.'product_types');
        }
        // check exist foreign key
        if ($this->checkExistFK_advertises($id)) {
            return array('-1', 'stores'.' '.t('is exist in').' '.'advertises');
        }
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getStoresById($id)
    {
        return $this->db->get_where($this->tablename, array('id' => $id));
    }
    
    public function getAllList()
    {
        return $this->db->get($this->tablename);
    }
    
    public function TotalStores()
    {
        return $this->db->count_all_results('Stores'); 
    } 
}       
        